package com.guiji.quartz.task;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.guiji.common.utils.spring.SpringUtils;
import com.guiji.quartz.domain.ChinaNews;
import com.guiji.quartz.domain.Financialexpress;
import com.guiji.quartz.domain.SpiderNews;
import com.guiji.quartz.service.ISpiderNewsService;
import com.guiji.quartz.service.RedisService;
import com.guiji.quartz.vo.ExcludeMovie;
import org.elasticsearch.client.license.LicensesStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: gaoX
 * @create: 2021-10-27 15:34
 **/
public class DataSaveUtil {
	private static final Logger log = LoggerFactory.getLogger(Financialexpress.class);

	public static void saveData(String title, String content, Object newImgs, String url, Date date, String source, String category) throws Exception {
		try {
			ISpiderNewsService service = SpringUtils.getBean(ISpiderNewsService.class);
			SpiderNews spider = new SpiderNews();
			spider.setNewsTitle(title);
			//去除停用词表
			ExcludeMovie excludeMovie = SpringUtils.getBean("excludeMovie");
			for (String word:excludeMovie.words) {
				if(title.toLowerCase().contains(word)){
					return;
				}
			}
			if(title.length()<4){
				return;
			}
			spider.setNewsContent(ChinaNews.filterSpecialCharacter(content));
			spider.setContentSize(spider.getNewsContent().length());
			JSONArray objects = JSONUtil.parseArray(JSONUtil.toJsonStr(newImgs));
			List<String> lists = JSONUtil.toList(objects, String.class);
			spider.setNewsUrl(url);
			spider.setNewsPublicTime(date);
			spider.setUpdateBy("admin");
			spider.setCreateTime(DateUtil.date());
			spider.setNewsSource(source);
			spider.setNewsCategory(category);

			RedisService redisService = SpringUtils.getBean(RedisService.class);
			Date ago = DateUtil.offsetDay(new Date(), -3);
			int i = date.compareTo(ago);
			//redis查询是否存在
			boolean ismember = redisService.isMember(SecureUtil.md5(title));
			if (!ismember && i > 0) {//如果不存在插入mysql和redis
				List<String> resImg = delPic(lists);
				spider.setNewsPicJson(JSONUtil.toJsonStr(resImg));
				service.save(spider);
				redisService.addTitleSet(SecureUtil.md5(title), spider.getNewsCategory(), date, title);
			}
		} catch (Exception e) {
			throw new Exception(url+"-插入数据库失败"+e);
		}
	}

	public static void saveData1(String title, String content, Object newImgs, String url, Date date, String source, String category,String description) throws Exception {
		try {
			ISpiderNewsService service = SpringUtils.getBean(ISpiderNewsService.class);
			SpiderNews spider = new SpiderNews();
			spider.setNewsTitle(title);
			//去除停用词表
			ExcludeMovie excludeMovie = SpringUtils.getBean("excludeMovie");
			for (String word:excludeMovie.words) {
				if(title.toLowerCase().contains(word)){
					return;
				}
			}
			if(title.length()<4){
				return;
			}
			spider.setNewsContent(ChinaNews.filterSpecialCharacter(content));
			spider.setContentSize(spider.getNewsContent().length());
			JSONArray objects = JSONUtil.parseArray(JSONUtil.toJsonStr(newImgs));
			List<String> lists = JSONUtil.toList(objects, String.class);
			spider.setNewsUrl(url);
			spider.setNewsPublicTime(date);
			spider.setUpdateBy("admin");
			spider.setCreateTime(DateUtil.date());
			spider.setNewsSource(source);
			spider.setNewsCategory(category);
			spider.setBackup2(description);

			RedisService redisService = SpringUtils.getBean(RedisService.class);
			Date ago = DateUtil.offsetDay(new Date(), -3);
			int i = date.compareTo(ago);
			//redis查询是否存在
			boolean ismember = redisService.isMember(SecureUtil.md5(title));
			if (!ismember && i > 0) {//如果不存在插入mysql和redis
				List<String> resImg = delPic(lists);
				spider.setNewsPicJson(JSONUtil.toJsonStr(resImg));
				service.save(spider);
				redisService.addTitleSet(SecureUtil.md5(title), spider.getNewsCategory(), date, title);
			}
		} catch (Exception e) {
			throw new Exception(url+"-插入数据库失败"+e);
		}
	}

	public static List<String> delPic(List<String> imgList) {
		List<String> newImg = new ArrayList<>();
		if(!ObjectUtil.isNull(imgList)&&!ObjectUtil.isEmpty(imgList)){
			if(imgList.get(0).contains("172.16.160.55")){
				return imgList;
			}
			for (Object img : imgList) {
				try {
					String name = UUID.randomUUID() + ".jpg";
					String picPath = "/data/cms/dist/pic/" + DateUtil.format(new Date(), "yyyy-MM-dd") + "/" + name;
					HttpResponse response = HttpRequest.get(img.toString()).setConnectionTimeout(15000)
						.execute();
					if(response.getStatus()==200){
						long l = response.writeBody(new File(picPath));
						//执行scp将图片拷贝到55节点
						String shell = "scp "+picPath+" root@172.16.160.55:/data/hdd/douyin/pic/";
						Process exec = Runtime.getRuntime().exec(shell, null, null);
						boolean tag = exec.waitFor(10, TimeUnit.SECONDS);
						if(l>10000&&tag==true){
							newImg.add("http://172.16.160.55/pic/" + name);
						}else {
							log.error("scp图片错误{}",DateUtil.now());
							continue;
						}
					}
				} catch (Exception e) {
					log.error("存储时处理图片错误{}",DateUtil.now());
					continue;
				}
			}
		}
		return newImg;
	}
}
